package com.warm.mini.wework.feign;

import com.alibaba.fastjson.JSONObject;
import com.warm.mini.wework.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description api feign impl
 * @Author warmchuang
 * @Date 2022/2/10 18:38
 */
@Slf4j
public class WeworkApiFeignImpl {

    private WeworkApiFeign weworkApiFeign;

    private WeworkApiFeignImpl(WeworkApiFeign weworkApiFeign) {
        this.weworkApiFeign = weworkApiFeign;
    }

    public String gettoken(String corpid, String corpsecret) {
        String accessToken = null;
        try {
            log.info("wework api-gettoken request: [corpid:{}, corpsecret:{}]", corpid, corpsecret);
            JSONObject resp = weworkApiFeign.gettoken(corpid, corpsecret);
            log.info("wework api-gettoken response:{}", resp);
            accessToken = resp.getString("access_token");
        } catch (Exception e) {
            log.info("wework api:gettoken error: {}", e.getMessage(), e);
        }
        return accessToken;
    }

    public static WeworkApiFeignImpl get() {
        return WeworkApiFeignImpl.WeworkApiEnum.INSTANCE.getInstance();
    }

    private enum WeworkApiEnum {
        INSTANCE;
        private WeworkApiFeignImpl instance;

        WeworkApiEnum() {
            instance = new WeworkApiFeignImpl(SpringContextUtil.getBean(WeworkApiFeign.class));
        }

        public WeworkApiFeignImpl getInstance() {
            return instance;
        }
    }
}
